#include<iostream>
#include<stack>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
	int m;
	while (cin >> m)
	{
		bool* ans = new bool[m];
		for (int i = 0; i < m; i++)
		{
			int n, e; cin >> n >> e;//n个结点，e条边
			int** input = new int* [e];
			for (int j = 0; j < e; j++)
				input[j] = new int[2];//初始化输入
			for (int j = 0; j < e; j++)
			{
				cin >> input[j][0] >> input[j][1];
				input[j][0]-=1;input[j][1]-=1;
				//每条边的出点和入点
			}
			int* innum = new int[n+1];//入度数组
			vector< vector<int> >edge;//设置结点对应边数组
			for (int tmp = 0; tmp < n+1; tmp++) 
			{
				innum[tmp] = 0;//初始化入度
				edge.push_back(vector<int>());
			}
			for (int j = 0; j < e; j++)
			{
				innum[input[j][1]]++;//设置入度
				edge[input[j][0]].push_back(input[j][1]);//有向
			}
			queue<int>storage; 
			for (int tmp = 1; tmp < n+1; tmp++) {
				if (innum[tmp] == 0)storage.push(innum[tmp]);//入度为0则入队
			}
			while (!storage.empty())
			{
				int tmp = storage.front();
				storage.pop();
				n--;//出队将结点数减1
				for (int tmp2 = 0; tmp2 < edge[tmp].size(); tmp2++)//遍历出队元素所在的边所对应的结点
				{
					if (--innum[edge[tmp][tmp2]] == 0)//入度减一，若为0则入队
						storage.push(edge[tmp][tmp2]);
				}
			}
			ans[i] = (n == 0) ? true : false;//false则有环
		}
		for (int i = 0; i < m; i++)
		{
			if (ans[i])cout << "NO" << endl;
			else cout << "YES" <<endl;

		}
	}
}